//
//  ConcurrentAlloc.h
//  ThreadCache
//
//  Created by 卜绎皓 on 2023/1/31.
//

#ifndef ConcurrentAlloc_h
#define ConcurrentAlloc_h


#endif /* ConcurrentAlloc_h */

#include "Common.hpp"
#include "ThreadCache.hpp"

static void* ConcurrentAlloc(size_t size)
{
    //通过TLS，每个线程无锁的获取自己专属的ThreadCache对象
    if (pTLSThreadCache == nullptr)
    {
        pTLSThreadCache = new ThreadCache;
    }
    
    cout << std::this_thread::get_id() << ":"<<pTLSThreadCache<<endl;

    return pTLSThreadCache->Allocate(size);
}

static void ConcurrentFree(void* ptr, size_t size)
{
    assert(pTLSThreadCache);

    pTLSThreadCache->Deallocate(ptr, size);
}
